<template>
    <div class="offline-class__wrapper">
        <intention-school-select :courseOfflineId="courseOfflineId" v-show="isIntentionSchool"
            @dataChange="handleOfflineClassChange"></intention-school-select>
        <intention-time-select v-show="isIntentionTime" @dataChange="handleOfflineClassChange"></intention-time-select>
        <student-select @dataChange="handleOfflineClassChange" ></student-select>
    </div>
</template>

<script>
import { OFFLINE_CLASS, OFFLINE_PERIOD, SENSORS_STUDENT_SELECTED_POPUP } from "../../utils/constants";
import IntentionSchoolSelect from "@xiaoe/xe-edu-poplar/lib/IntentionSchoolSelect/index.js";
import IntentionTimeSelect from "@xiaoe/xe-edu-poplar/lib/IntentionTimeSelect/index.js";
import StudentSelect from "@xiaoe/xe-edu-poplar/lib/StudentSelect/index.js";
// import { sensorsTrackButton } from "../../utils/sensors_report";
import { checkOLCDuplicateOrder } from "../../fetch-data";

export default {
    props: {
        businessConf: Object,
        spuInfo: Object,
    },
    components: {
        IntentionSchoolSelect,
        IntentionTimeSelect,
        StudentSelect,
    },
    data() {
        return {
            eduStudentInfo: {
                intention_school: "",
                intention_time: "",
                school_id: "",
                course_id: "",
                student_id: "",
            },
            OFFLINE_CLASS,
            OFFLINE_PERIOD,
            EDU_STUDENT_INFO_MAP: {
                "intention_school": "意向上课校区",
                "intention_time": "意向上课时间",
                "student_id": "学员信息",
            }
        }
    },
    methods: {
        handleDataChange() {
            this.$emit("change", this.eduStudentInfo);
        },
        checkDuplicateOrder() {
            //只有是班级售卖才校验
            if (!(this.specType === 3)) {
                return Promise.resolve(true);
            }
            const params = {
                student_id: this.eduStudentInfo.student_id,
                class_id: this.classId
            }
            if (!params.student_id || !this.classId) {
                return Promise.resolve(false);
            }
            return checkOLCDuplicateOrder(params)
                .then(result => {
                    if (result[0]) {
                        // todo
                        // this.$toast(`您选择的学员已购买了该一对多课程，暂不支持重复购买`);
                        return Promise.reject("您选择的学员已购买了该一对多课程，暂不支持重复购买");
                    }
                    return true
                })
        },
        // handleSensorsReport() {
        //     sensorsTrackButton({
        //         page_id: SENSORS_STUDENT_SELECTED_POPUP[this.spuInfo.spu_type],
        //         goods_id: this.spuInfo.spu_id,
        //         page_button: '立即添加',
        //         spu_type: this.spuInfo.spu_type
        //     })
        // },
        handleOfflineClassChange(key, info) {
            this.eduStudentInfo[key] = info;
            this.handleDataChange()
        },
        validate() {
            const errMsg = this.errors[0];
            if(errMsg) {
                return Promise.reject(errMsg);
            }
            return this.checkDuplicateOrder();
        }
    },

    computed: {
        // 线下课/课时包业务信息
        OLCConf() {
            return this.businessConf[this.spuInfo.spu_type];
        },
        specType() {
            return this.OLCConf && this.OLCConf.spec_type;
        },
        isIntentionTime() {
            if (!this.OLCConf) {
                return false
            }
            return Boolean(this.OLCConf.is_intention_time);
        },
        isIntentionSchool() {
            if (!this.OLCConf) return false
            return Boolean(this.OLCConf.is_intention_address);
        },
        courseId() {
            return this.OLCConf && this.OLCConf.course_id;
        },
        classId() {
            if (this.OLCConf && Array.isArray(this.OLCConf.sku_list)) {
                const list = this.OLCConf.sku_list
                let skuInfo = list.find(item => item.sku_id === this.spuInfo.sku)
                if (skuInfo) {
                    return skuInfo.spec_value
                }
                return ''
            }
            return ''
        },
        courseOfflineId() {
            return this.OLCConf && this.OLCConf.course_offline_id;
        },
        errors() {
            let checkMsgArr = [
                {
                    key: 'student_id',
                    msg: '请选择学员信息'
                }
            ]
            const isIntentionTime = this.isIntentionTime;
            const isIntentionSchool = this.isIntentionSchool;
            isIntentionTime && checkMsgArr.unshift({
                key: 'intention_time',
                msg: '请选择意向上课时间'
            });
            isIntentionSchool && checkMsgArr.unshift({
                key: 'intention_school',
                msg: '请选择意向上课校区'
            });
            const data = this.eduStudentInfo;
            return checkMsgArr.reduce((result, cur) => {
                const { key, msg } = cur
                if (data[key] == '' || data[key] == null) {
                    result.push(msg)
                }
                return result
            }, [])
        }
    },
    watch: {
        courseId: {
            handler(val) {
                this.eduStudentInfo.course_id = val;
            },
            immediate: true
        },
        errors() {
            this.showOfflineExpandInfo && this.handleDataChange();
        }
    }
}
</script>

<style lang="scss" scope>
.offline-class {
    &__wrapper {
        margin-bottom: .2rem;
    }
}
</style>